/**
 * 
 */
package com.mycompany.client.pages;

import java.util.Date;
import java.util.Map;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mycompany.client.CoralPage;
import com.mycompany.client.async.CalendarServiceAsync;
import com.mycompany.client.async.ServiceFactory;
import com.mycompany.client.dataset.CalendarDataSource;
import com.mycompany.client.model.CalendarBO;
import com.mycompany.client.widgets.CoralWindow;
import com.mycompany.client.widgets.form.CoralRightButtonForm;
import com.mycompany.client.widgets.layout.CoralBasicVLayout;
import com.mycompany.shared.PageConstants;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.calendar.Calendar;
import com.smartgwt.client.widgets.calendar.CalendarEvent;
import com.smartgwt.client.widgets.calendar.events.CalendarEventAdded;
import com.smartgwt.client.widgets.calendar.events.CalendarEventChangedEvent;
import com.smartgwt.client.widgets.calendar.events.CalendarEventClick;
import com.smartgwt.client.widgets.calendar.events.CalendarEventRemoved;
import com.smartgwt.client.widgets.calendar.events.EventAddedHandler;
import com.smartgwt.client.widgets.calendar.events.EventChangedHandler;
import com.smartgwt.client.widgets.calendar.events.EventClickHandler;
import com.smartgwt.client.widgets.calendar.events.EventRemovedHandler;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;

/**
 * @author coral.ma
 *
 */
public class CalendarPage extends CoralPage {
	
	private String NAME = "Calendar";
	private String DESCRIPTION = "Sales Activity Calendar";
	public CalendarServiceAsync calendarService = ServiceFactory.getCalendarService();
	
	public static CoralPage create() {
		CalendarPage activityPage = new CalendarPage();
		return activityPage;
	}
	
	public Canvas getViewPage() {
		CoralBasicVLayout layout = new CoralBasicVLayout(PageConstants.CalendarPage);
		final Calendar calendar = new Calendar();
		calendar.addEventAddedHandler(new EventAddedHandler() {
			
			public void onEventAdded(CalendarEventAdded event) {
				CalendarBO calendarBO = fillCalendarBO(event.getEvent()); 
				calendarService.addCalendar(calendarBO, new AsyncCallback<CalendarBO>() {
					public void onSuccess(CalendarBO result) {
						System.out.println("Add Successful! The Calendar ID is " + result.getEventId());
					}
					public void onFailure(Throwable caught) {
						System.out.println(caught.getMessage());
					}
				});
			}
		});
		calendar.addEventClickHandler(new EventClickHandler() {
			public void onEventClick(CalendarEventClick event) {
				final CalendarEvent calendarEvent = event.getEvent();
				CalendarBO bo = fillCalendarBO(calendarEvent);
				System.out.println("EventClickHandler");
				Button closeBtn = new Button("Close");
				final CoralWindow window = new CoralWindow(PageConstants.AddSalesActivityPage,closeBtn);
				window.init(bo);
				closeBtn.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						Map values = window.getValues();
						Date plannedDate = (Date) values.get("plannedDate");
						Date actualDate = (Date) values.get("actualDate");
						String customerName = (String)values.get("customerName");
						System.out.println(plannedDate);
						System.out.println(actualDate);
						System.out.println(customerName);
//						calendar.addEvent(plannedDate,actualDate,customerName,customerName);
						calendarEvent.setStartDate(plannedDate);
						calendarEvent.setEndDate(actualDate);
						calendarEvent.setName(customerName);
						window.destroy();
//						calendar.addEvent(new Date(new Long("1274952000000")), new Date(new Long("1274954000000")), "Coral Testing", "Just Testing");
					}
				});
				window.show();
				System.out.println("Add Event");
			}
		});
		calendar.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				System.out.println("Calendar Click Handler");				
			}
		});
		calendar.addEventChangedHandler(new EventChangedHandler() {
			public void onEventChanged(CalendarEventChangedEvent event) {
				CalendarBO calendarBO = fillCalendarBO(event.getEvent());
				calendarService.updateCalendar(calendarBO, new AsyncCallback<CalendarBO>() {
					public void onSuccess(CalendarBO result) {
						System.out.println("Change Successful! The Calendar ID is " + result.getEventId());
					}
					public void onFailure(Throwable caught) {
						System.out.println(caught.getMessage());
					}
				});
				System.out.println("EventChangeHandler");
			}
		});
		calendar.addEventRemovedHandler(new EventRemovedHandler() {
			
			public void onEventRemoved(CalendarEventRemoved event) {
				CalendarBO calendarBO = fillCalendarBO(event.getEvent());
				calendarService.deleteCalendar(calendarBO, new AsyncCallback<Boolean>() {
					
					public void onSuccess(Boolean result) {
						System.out.println("Delete Successful!");
					}
					public void onFailure(Throwable caught) {
						System.out.println(caught.getMessage());
					}
				});
				System.out.println("EventRemovedHandler");
				
			}
		});
		CalendarDataSource calendarDataSource = new CalendarDataSource();
	    calendar.setDataSource(calendarDataSource);
	    calendar.setShowWeekends(true);  
	    calendar.setAutoFetchData(true);  
	    calendar.setEventAutoArrange(true);  
	    calendar.setEventOverlap(false);
	    
	    calendar.setShowQuickEventDialog(false);
		calendar.setShowDragShadow(true);
		calendar.setCanCreateEvents(false);
		calendar.setCanEditEvents(false);
	    
	    layout.addMember(calendar);
	    
	    Button addActivityBtn = new Button("Add Activity");
	    addActivityBtn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Button closeBtn = new Button("Close");
				final CoralWindow window = new CoralWindow(PageConstants.AddSalesActivityPage,closeBtn);
				window.init();
				closeBtn.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						Map values = window.getValues();
						Date plannedDate = (Date) values.get("plannedDate");
						Date actualDate = (Date) values.get("actualDate");
						String customerName = (String)values.get("customerName");
						System.out.println(plannedDate);
						System.out.println(actualDate);
						System.out.println(customerName);
						calendar.addEvent(plannedDate,actualDate,customerName,customerName);
						window.destroy();
//						calendar.addEvent(new Date(new Long("1274952000000")), new Date(new Long("1274954000000")), "Coral Testing", "Just Testing");
					}
				});
				window.show();
				System.out.println("Add Event");
			}
		});
	    CoralRightButtonForm buttonForm = new CoralRightButtonForm();
	    buttonForm.addButtons(addActivityBtn);
	    layout.addMember(buttonForm);
		return layout;
	}
	
	private CalendarBO fillCalendarBO(CalendarEvent event) {
		int eventId = event.getEventId();
		Date startDate = event.getStartDate();
		Date endDate = event.getEndDate();
		String name = event.getName();
		String description = event.getDescription();
		return new CalendarBO(eventId,startDate,endDate,name,description);
	}

	public String getPageDescription() {
		return DESCRIPTION;
	}

	public String getPageName() {
		return NAME;
	}

	public String getIcon() {
		return null;
	}
}
